package org.grow.sha.serviceAop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.grow.sha.service.TaskExeService;
import org.grow.sha.tool.RobotEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

import static org.grow.sha.service.TaskExeService.*;

@Component
@Aspect
public class SgsAdvice {
    Logger logger = LoggerFactory.getLogger(SgsAdvice.class);
    @Autowired
    private RobotEx robot;
    @Pointcut("@annotation(org.grow.sha.serviceAop.Modal)")
//    @Pointcut("@annotation(Modal)")
    public void modalPointcut(){

    }
    @Around("modalPointcut()")
    public Object modalAdvice(ProceedingJoinPoint joinPoint)
            throws Throwable {
        TimeUnit.SECONDS.sleep(1);
//        System.out.println("modal切面织入成功");
        Object proceed = joinPoint.proceed();
        robot.delay(1000);
        robot.moveAndClick(relaxPoint.x, relaxPoint.y);
        return proceed;
    }

    @Pointcut("@annotation(Tab)")
    public void tabPointcut(){}
    @Around("tabPointcut()")
    public Object tabAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
//        System.out.println("tab切面织入成功");
        Object proceed = joinPoint.proceed();
        int timeout = 20;
        Color target = robot.getPixelColor(corePoint.x,
                corePoint.y);
        if (!target.equals(mainmenu)) {
            while (true) {
                robot.mouseMove(relaxPoint.x, relaxPoint.y);
                robot.delay(1000);
                if (robot.getPixelColor(corePoint.x, corePoint.y).equals(mainmenu)) {
                    break;
                } else {
                    timeout--;
                    if (timeout == 1) {
                        logger.debug("返回主页面出错，超时异常，请排查");
//                        todo:切面的异常捕获 问题 通常是先写连接点 再写调用端 再写切面 ，如果切面主动抛出异常 那么调用端如何修改代码
                        throw new BackMenuException("超时异常");
                    }
                    robot.delay(1000);
                    robot.moveAndClick(corePoint.x, corePoint.y);
                }
            }
        }
//此处是死循环　保障手工能回到主页面
        robot.delay(1000);
        return proceed;
    }
    @Value("${error.shotsdir}")
    private String errorPicDir;
    public static final Color mainTreeColor = new Color(84,36,0);
    public static final Point mainTreePoint = new Point(1051,543);
    @Pointcut("@annotation(ValidatePosition)")
    public void valiPosition(){};
    @Before("valiPosition()")
    public void validatePositionAdvice(JoinPoint joinPoint) throws ValidateMenuException {
//注意 有时候会因为将灵的动画动作 导致颜色被覆盖
//        这里选择另一套方案颜色 不再验证mainmenu
// 主界面右侧树的颜色       new Color(84,36,0) 1051 543
        Color c = robot.getPixelColor(mainTreePoint.x,mainTreePoint.y);
        if (!c.equals(mainTreeColor)){
            robot.delay(2000);
        }else {
            return;
        }
        c = robot.getPixelColor(mainTreePoint.x,mainTreePoint.y);
//        c = robot.getPixelColor(corePoint.x, corePoint.y);

        if (!robot.veryCloseTo(mainTreeColor,c)){
            String name = joinPoint.getSignature().getName();
            long l = System.currentTimeMillis();
            String filename = l+name+".png";
            logger.warn("主页面验证不通过,目前不能自动纠错,将要抛出ValidateMenuE 84,36,0 -> "+c);
            try {
                robot.createErrorSnapshot(
                        new File(errorPicDir+"/"+filename));
                logger.debug("待排查的图像: filename: "+filename);
            } catch (IOException e) {
                e.printStackTrace();
            }
            throw new ValidateMenuException("将要进行的环节 "+name);
        }
    }
}
